package com.example.test.dialogfactory

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import easier.app.x.view.GridLayoutSpaceSpreadDecoration
import easier.log.logger.Logger
import easier.recycler.DataViewHolder
import easier.recycler.SingleTypeAdapter

class MainActivity21_2 : AppCompatActivity() {

    private var mRecycler: RecyclerView? = null

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, MainActivity21_2::class.java)
            context.startActivity(starter)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main212)
        initView()
    }

    private fun initView() {
        val spanCount = 5
        mRecycler = findViewById<View>(R.id.recycler) as RecyclerView
        mRecycler!!.layoutManager = GridLayoutManager(this, spanCount)
        val adapter: SingleTypeAdapter<String> = object : SingleTypeAdapter<String>() {
            override fun buildHolder(position: Int, t: String): DataViewHolder<String> {
                return object : DataViewHolder<String>(R.layout.activity_main_21_item, t) {
                    override fun onBind(
                        viewHolder: RecyclerView.ViewHolder,
                        root: View,
                        position: Int,
                        type: Int,
                        payloads: List<Any>
                    ) {
                        root.post { Logger.d("$position $root") }
                    }
                }
            }
        }
        mRecycler!!.adapter = adapter
        val data: MutableList<String> = ArrayList()
        for (i in 0 until spanCount * 3) {
            data.add("")
        }
        adapter.updateSource(data)
        val decoration = GridLayoutSpaceSpreadDecoration(spanCount, 20f)
        mRecycler!!.addItemDecoration(decoration)
    }
}